<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;

use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Widgets\Table;
use App\Admin\Models\walletTrade;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use DB;
use Ixudra\Curl\Facades\Curl;

class WalletTradeController extends Controller
{
	use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('流水记录')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('流水记录')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('账本')
            ->description('手工帐')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new walletTrade);
        $grid->wtId('ID');
        $grid->wtChangedMoney('金额');
        $grid->wtType('类型')->display(function($type){
			return $type=='charge' ? '入账' : '出账';
		});
//      $grid->wtAbstract('交易摘要');
		$grid->wtAbstract('交易摘要')->expand(function ($model) {

            if ($this->tradeColumn == '无') {
                return "";
            }

			if ($this->wtObj == 'shareBonus'){
				$dataArray = [
	            				[
		            				$this->tradeInfo->slId,
		            				$this->tradeInfo->sbMonth,
		            				$this->tradeInfo->totalProfit,
		            				$this->tradeInfo->sbShareMoney,
		            				$this->tradeInfo->sbTransferMoney,
	            				]
	            			 ];


	            return new Table(['ID', '分红月', '当月利润', '分红总金额', '保留金额'], $dataArray);
	        } elseif ($this->wtObj == 'getSettleTip'){
	        	$dataArray = [
	            				[
		            				$this->tradeInfo->sclId,
		            				$this->tradeInfo->sgName,
		            				$this->tradeInfo->muNickName,
		            				$this->tradeInfo->name,
		            				$this->tradeInfo->sclParts,
		            				$this->tradeInfo->sgServiceMoney,
		            				$this->tradeInfo->sclParts,
		            				$this->tradeInfo->createUser,
		            				$this->tradeInfo->createTime,
	            				]
	            			 ];


	            return new Table(['ID','卡项名称', '客户姓名', '服务人', '服务项目', '服务费用', '记录时间', '记录人', '记录时间'], $dataArray);
			} elseif ($this->wtObj == 'goods') {
				if (empty($this->sgoInfo)){
					return '';
				}
				$sgoDataArray = [
							     [
							     	$this->sgoInfo->sgoId,
							     	$this->sgoInfo->sgName,
							     	$this->sgoInfo->sgoSaleNum,
							     	$this->sgoInfo->sgoSalePrice,
							     	$this->sgoInfo->sgoCost,
							     	$this->sgoInfo->muNickName,
                                    $this->sgoInfo->name,
                                    $this->sgoInfo->sgoCommission,
							     	$this->sgoInfo->sgoMemo,
							     	$this->sgoInfo->sgoPayWay,
							     ]
							 ];

				$sgoTable = new Table(['ID','商品', '售出数量', '售出价格', '商品成本', '购买人', '销售', '销售返利', '备注', '支付方式'], $sgoDataArray);
			    if (empty($this->disInfo)){
			   		return $sgoTable;
			   	}
			    $saleMoney = $this->disInfo->dbSaleMoney;
			    if ($this->disInfo->dbActualMoney && $this->disInfo->dbActualMoney != $this->disInfo->dbSaleMoney){
			    	$saleMoney = $saleMoney."(实际支付：{$this->disInfo->dbActualMoney})";
			    }

				$disDataArray = [
							     [
							     	$this->disInfo->dbId,
							     	//$this->disInfo->dbObjName,
							     	//$this->disInfo->dbSaleNum,
							     	$saleMoney,
							     	$this->disInfo->userName0."【{$this->disInfo->alName0}】",
							     	$this->disInfo->userName1."【{$this->disInfo->alName1}】",
							     	$this->disInfo->dbSelfMoney,
							     	$this->disInfo->userName2."【{$this->disInfo->alName2}】",
							     	$this->disInfo->dbParentMoney,
							     	$this->disInfo->userName3."【{$this->disInfo->alName3}】",
							     	$this->disInfo->dbGrandMoney,
							     ]
							 ];
							 //'商品', '售出数量',
				$disTable = new Table(['ID','分销金额','购买人', '推荐人', '推荐人返利', '推荐人上级', '推荐人返利', '推荐人上上级', '上上级返利'], $disDataArray);
				return $sgoTable.''.$disTable;
			} elseif ($this->wtObj == 'cardService') {
				$sclDataArray = [];

				foreach($this->sclInfo as $sclInfo){
					$sclArray = [];
					array_push($sclArray, $sclInfo->sclId);
					array_push($sclArray, $sclInfo->sgName);
					array_push($sclArray, $sclInfo->muNickName);
					array_push($sclArray, $sclInfo->sclParts);
					array_push($sclArray, $sclInfo->name);
					array_push($sclArray, $sclInfo->createTime);
					array_push($sclDataArray, $sclArray);
				}

				$sclTable = new Table(['ID','卡项名称', '用户名', '服务项目', '美容师', '记录时间'], $sclDataArray);
			    return $sclTable;
			} elseif ($this->wtObj == 'recharge' || $this->wtObj == 'consumption') {
				if (empty($this->sgoList)){
					return '';
				}
				$sgoDataArray = [];

				foreach($this->sgoList as $sgoInfo){
					$sgoArray = [];
					array_push($sgoArray, $sgoInfo->sgoId);
					array_push($sgoArray, $sgoInfo->sgName);
					array_push($sgoArray, $sgoInfo->sgoSaleNum);
					array_push($sgoArray, $sgoInfo->sgoSalePrice);
					array_push($sgoArray, $sgoInfo->sgoCost);
					array_push($sgoArray, $sgoInfo->muNickName);
					array_push($sgoArray, $sgoInfo->sgoMemo);
					array_push($sgoArray, $sgoInfo->sgoPayWay);
					array_push($sgoDataArray, $sgoArray);
				}
				$sgoTable = new Table(['ID','商品', '售出数量', '售出价格', '商品成本', '购买人', '备注', '支付方式'], $sgoDataArray);
				return $sgoTable;
			} elseif ($this->wtObj == 'goodsDis') {
				$saleMoney = $this->disInfo->dbSaleMoney;
			    if ($this->disInfo->dbActualMoney && $this->disInfo->dbActualMoney != $this->disInfo->dbSaleMoney){
			    	$saleMoney = $saleMoney."(实际支付：{$this->disInfo->dbActualMoney})";
			    }

				$disDataArray = [
							     [
							     	$this->disInfo->dbId,
							     	$this->disInfo->dbObjName,
							     	$this->disInfo->dbSaleNum,
							     	$saleMoney,
							     	$this->disInfo->userName0."【{$this->disInfo->alName0}】",
							     	$this->disInfo->userName1."【{$this->disInfo->alName1}】",
							     	$this->disInfo->dbSelfMoney,
							     	$this->disInfo->userName2."【{$this->disInfo->alName2}】",
							     	$this->disInfo->dbParentMoney,
							     	$this->disInfo->userName3."【{$this->disInfo->alName3}】",
							     	$this->disInfo->dbGrandMoney,
							     ]
							 ];

				$disTable = new Table(['ID', '商品', '售出数量', '分销金额','购买人', '推荐人', '推荐人返利', '推荐人上级', '推荐人返利', '推荐人上上级', '上上级返利'], $disDataArray);
				return $$disTable;
			} elseif ($this->wtObj == 'a') {

			} elseif ($this->wtObj == 'a') {

			} else {
				return "";
			}

        });
        $grid->wtCost('成本');
        $grid->wtProfit('利润');
        $grid->createTime('创建时间');
        $grid->createUser('创建人');

		$grid->header(function ($query) {
			$sId = Admin::user()->sId;
			$sInfo = DB::table('store')->where("sId", $sId)->first();
			$host = config('admin.dataApiHost');
			$accountUrl = $host."/wallet/getAccountInfo?wcusKey={$sInfo->sId}&uId={$sInfo->sCode}";
			$data = Curl::to($accountUrl)->get();

			try {
			     $data = json_decode($data, true);
			     if ($data['code'] == 0){
			     	$accountMoney = $data['msg'];
			     }else{
			     	$accountMoney = $data['data']['waMoney'];
			     }
			} catch (Exception $e) {
			     $accountMoney = '获取账户信息错误！';
			}
		    return "<div style='padding: 10px;'><strong>当前账户余额 ：<a style='color: red;font-size: 20px;'> {$accountMoney}</a></strong></div>";
		});


		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();

		});

		$grid->actions(function ($actions) {
		    $actions->disableDelete();
		    $actions->disableEdit();
			$actions->disableView();
		});

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();

		    $filter->column(1/2, function ($filter) {

		    	$filter->like('sgName', '产品名称');

			});

		});

		// 默认展开筛选框
		$grid->expandFilter();

		// 禁用行选择checkbox
		$grid->disableRowSelector();

		// 禁用导出
		$grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$walletTrade = new walletTrade();
        $show = new Show($walletTrade->findOrFail($id));

        //$show->spId('ID');
        $show->spType('参数类型');
        $show->spKey('参数名称');
        $show->spValue('参数值');
        $show->spMemo('参数备注');
        $show->spSort('参数排序');
		$show->panel()
			->tools(function ($tools) {
//				$tools->disableEdit();
//				$tools->disableList();
				$tools->disableDelete();
		});;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::Form(walletTrade::class, function (Form $form) {

			$now = date('YmdHis', time());

			$form->text('wtOrderCode', '交易单号')->default($now);
			$form->number('wtChangedMoney', '金额')->default(0)->help('记账金额，可以填写负数，代表门店支出或者冲账');
			$form->text('wtAbstract', '摘要')->placeholder('请填写因何而记账')->rules("required", ['required' => '请填写交易摘要']);
			$form->text('wtMemo', '备注');

            $form->tools(function (Form\Tools $tools) {

			    // 去掉`列表`按钮
//			    $tools->disableList();

			    // 去掉`删除`按钮
			    $tools->disableDelete();

			    // 去掉`查看`按钮
			    $tools->disableView();

			    // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//			    $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
			});

			$form->footer(function ($footer) {

//			    // 去掉`重置`按钮
//			    $footer->disableReset();
//
//			    // 去掉`提交`按钮
//			    $footer->disableSubmit();

			    // 去掉`查看`checkbox
			    $footer->disableViewCheck();

			    // 去掉`继续编辑`checkbox
			    $footer->disableEditingCheck();

			    // 去掉`继续创建`checkbox
			    $footer->disableCreatingCheck();

			});
        });
    }
}
